import {
  DeviceEventEmitter,
  EmitterSubscription,
  NativeModules,
  NativeEventEmitter
} from 'react-native';
import protobufjs from 'protobufjs';

import connection from '../../service/connection';
import ground from '../../service/ground1';
import { REACT_NATIVE_PUSH_MSG } from '../../constant/nativeEvent';
import { isIOS } from '../../constant';
import {
  PUSH_MSG_ID_INDEX,
  FINISH_PROBLEM,
  FINISH_DISCUSS,
  START_DISCUSS,
  USER_LEAVE_ROOM,
  LESSON_START,
  USER_JOIN_ROOM,
  LESSON_ASSIST_START_TALK,
  LESSON_ASSIST_STOP_TALK,
  ASSISTANT_START_TALK_TO_STUDENT,
  ASSISTANT_STOP_TALK_TO_STUDENT,
  FINISH_INSTANT,
  NEW_MESSAGE,
  WITH_DRAW_MESSAGE,
  LESSON_SIGN,
  SHOW_FACE,
  READ_MESSAGE,
  TEACHER_AUDIO
} from '../../service/constant';
import Log from '../../common/log';
import { AppApi } from '../../api';

const { RNSendEvent } = NativeModules;
const RNSendEventEmitter = new NativeEventEmitter(RNSendEvent);

// 用户完成互动题通知
export const CLASS_FINISH_PROBLEM = 'CLASS_FINISH_PROBLEM';

// 完成讨论环节通知
export const CLASS_FINISH_DISCUSS = 'CLASS_FINISH_DISCUSS';

// 开始讨论环节通知
export const CLASS_START_DISCUSS = 'CLASS_START_DISCUSS';

// 其他用户加入房间通知
export const CLASS_USER_JOIN_ROOM = 'CLASS_USER_JOIN_ROOM';

// 其他用户离开房间通知
export const CLASS_USER_LEAVE_ROOM = 'CLASS_USER_LEAVE_ROOM';

// 课程开始通知
export const CLASS_LESSON_START = 'CLASS_LESSON_START';

// 辅导老师开始讲话通知
export const CLASS_LESSON_ASSIST_START_TALK = 'CLASS_LESSON_ASSIST_START_TALK';

// 辅导老师结束讲话通知
export const CLASS_LESSON_ASSIST_STOP_TALK = 'CLASS_LESSON_ASSIST_STOP_TALK';

// 辅导老师开始同单个学生讲话通知
export const CLASS_ASSISTANT_START_TALK_TO_STUDENT =
  'CLASS_ASSISTANT_START_TALK_TO_STUDENT';

// 辅导老师结束同单个学生讲话通知
export const CLASS_ASSISTANT_STOP_TALK_TO_STUDENT =
  'CLASS_ASSISTANT_STOP_TALK_TO_STUDENT';

// 用户完成即兴题通知
export const CLASS_FINISH_INSTANT = 'CLASS_FINISH_INSTANT';

// 新消息通知
export const CLASS_NEW_MESSAGE = 'CLASS_NEW_MESSAGE';

// 撤回消息通知
export const CLASS_WITH_DRAW_MESSAGE = 'CLASS_WITH_DRAW_MESSAGE';

// 上课签到通知
export const CLASS_LESSON_SIGN = 'CLASS_LESSON_SIGN';

// 露脸加金币通知
export const CLASS_SHOW_FACE = 'CLASS_SHOW_FACE';

// 阅读消息通知
export const CLASS_READ_MESSAGE = 'CLASS_READ_MESSAGE';

// 老师语音通知
export const CLASS_TEACHER_AUDIO = 'CLASS_TEACHER_AUDIO';

interface PushData {
  content: string;
  taskId: number;
}

let pushMsgSubscription: EmitterSubscription;

const getData = (res: PushData): void => {
  try {
    const { content } = res;

    const cPacketRoot = protobufjs.Root.fromJSON(connection);
    const root = protobufjs.Root.fromJSON(ground);

    const RspMsg = cPacketRoot.lookupType('client.CPacketNotify');
    const rspMsg: any = RspMsg.decode(new Uint8Array(JSON.parse(content)));
    const { notifyMsgId, notifyMsgBody } = rspMsg;
    const RspData = root.lookupType(
      `beeground.${PUSH_MSG_ID_INDEX[Number(notifyMsgId)]}`
    );
    const pushData = RspData.decode(new Uint8Array(notifyMsgBody));

    Log.i(
      'get log push msg-- ',
      `notifyMsgId:${notifyMsgId} pushData:${JSON.stringify(pushData)}`
    );

    AppApi.reportArrivedPushMsg({ msgId: notifyMsgId });

    switch (notifyMsgId) {
      case FINISH_PROBLEM:
        DeviceEventEmitter.emit(CLASS_FINISH_PROBLEM, pushData);
        break;
      case FINISH_DISCUSS:
        DeviceEventEmitter.emit(CLASS_FINISH_DISCUSS, pushData);
        break;
      case START_DISCUSS:
        DeviceEventEmitter.emit(CLASS_START_DISCUSS, pushData);
        break;
      case USER_JOIN_ROOM:
        DeviceEventEmitter.emit(CLASS_USER_JOIN_ROOM, pushData);
        break;
      case USER_LEAVE_ROOM:
        DeviceEventEmitter.emit(CLASS_USER_LEAVE_ROOM, pushData);
        break;
      case LESSON_START:
        DeviceEventEmitter.emit(CLASS_LESSON_START, pushData);
        break;
      case LESSON_ASSIST_START_TALK:
        DeviceEventEmitter.emit(CLASS_LESSON_ASSIST_START_TALK, pushData);
        break;
      case LESSON_ASSIST_STOP_TALK:
        DeviceEventEmitter.emit(CLASS_LESSON_ASSIST_STOP_TALK, pushData);
        break;
      case ASSISTANT_START_TALK_TO_STUDENT:
        DeviceEventEmitter.emit(
          CLASS_ASSISTANT_START_TALK_TO_STUDENT,
          pushData
        );
        break;
      case ASSISTANT_STOP_TALK_TO_STUDENT:
        DeviceEventEmitter.emit(CLASS_ASSISTANT_STOP_TALK_TO_STUDENT, pushData);
        break;
      case FINISH_INSTANT:
        DeviceEventEmitter.emit(CLASS_FINISH_INSTANT, pushData);
        break;
      case NEW_MESSAGE:
        DeviceEventEmitter.emit(CLASS_NEW_MESSAGE, pushData);
        break;
      case WITH_DRAW_MESSAGE:
        DeviceEventEmitter.emit(CLASS_WITH_DRAW_MESSAGE, pushData);
        break;
      case LESSON_SIGN:
        DeviceEventEmitter.emit(CLASS_LESSON_SIGN, pushData);
        break;
      case SHOW_FACE:
        DeviceEventEmitter.emit(CLASS_SHOW_FACE, pushData);
        break;
      case READ_MESSAGE:
        DeviceEventEmitter.emit(CLASS_READ_MESSAGE, pushData);
        break;
      case TEACHER_AUDIO:
        DeviceEventEmitter.emit(CLASS_TEACHER_AUDIO, pushData);
        break;
      default:
        break;
    }
  } catch (e) {}
};

export const startReceivePushMsg = (): void => {
  if (!isIOS) {
    pushMsgSubscription = DeviceEventEmitter.addListener(
      REACT_NATIVE_PUSH_MSG,
      getData
    );
  } else {
    RNSendEventEmitter.addListener(REACT_NATIVE_PUSH_MSG, getData);
  }
};

export const stopReceivePushMsg = (): void => {
  if (!isIOS) {
    if (pushMsgSubscription) {
      pushMsgSubscription.remove();
    }
  } else {
    RNSendEventEmitter.removeAllListeners();
  }
};
